45. GUI in PowerShell

Note

The below information is extensively based in information taken from the PowerShell® Notes for Professionals book. I plan to extend this information based on my day to day usage of the language.

45.1: WPF GUI for Get-Service cmdlet

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Add- Type - AssemblyName PresentationFramework

[xml]$XAMLWindow = '
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="Auto"
    SizeToContent="WidthAndHeight"
    Title="Get-Service">
    <ScrollViewer Padding="10,10,10,0" ScrollViewer.VerticalScrollBarVisibility="Disabled">
        <StackPanel>
            <StackPanel Orientation="Horizontal">
                <Label Margin="10,10,0,10">ComputerName:</Label>
                <TextBox Name="Input" Margin="10" Width="250px"></TextBox>
            </StackPanel>
            <DockPanel>
                <Button Name="ButtonGetService" Content="Get-Service" Margin="10" Width="150px"
                IsEnabled="false"/>
                <Button Name="ButtonClose" Content="Close" HorizontalAlignment="Right" Margin="10"
                Width="50px"/>
            </DockPanel>
        </StackPanel>
    </ScrollViewer >
    </Window>
'
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Create the Window Object
$Reader = (New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $XAMLWindow)
$Window = [Windows.Markup.XamlReader]::Load( $Reader )

# TextChanged Event Handler for Input
$TextboxInput = $Window.FindName('Input')

$TextboxInput.add_TextChanged.Invoke(
  {
    $ComputerName = $TextboxInput.Text
    $ButtonGetService.IsEnabled = $ComputerName -ne ''
  }
)
# Click Event Handler for ButtonClose
$ButtonClose = $Window.FindName('ButtonClose')

$ButtonClose.add_Click.Invoke(
  {
    $Window.Close()
  }
)
# Click Event Handler for ButtonGetService
$ButtonGetService = $Window.FindName('ButtonGetService')
$ButtonGetService.add_Click.Invoke(
  {
    $ComputerName = $TextboxInput.text.Trim()
    try
    {
      Get-Service -ComputerName $ComputerName | Out-GridView -Title "Get-Service on $ComputerName"
    }
    catch
    {
      [System.Windows.MessageBox]::Show($_.exception.message,'Error',[System.Windows.MessageBoxButton]::OK,[System.Windows.MessageBoxImage]::Error)
    }
  }
)
# Open the Window
$null = $Window.ShowDialog()

This creates a dialog window which allows the user to select a computer name, then will display a table of services and their statuses on that computer.

This example uses WPF rather than Windows Forms.